<?php
/**
 * 项目动作相关操作
 * @author qianxun
 *
 */
class AdminProAct extends MY_Controller{
    function AdminProAct(){
        parent::MY_Controller();
    }
    /**
     * 新增或删除动作
     */
    function add(){
        $this->load->model('proActModel');
        if ($_POST){
            $actId = (int)$this->input->post('actId');
            $data = array(
                'proId' => (int)$this->input->post('proId'),
                'actTitle' => $this->input->post('actTitle'),
                'actDesc' => $this->input->post('actDesc'),
                'orderNum' => (int)$this->input->post('orderNum'),
                'isDel' => 0,
            );
            $data['proId'] || showError('请选择添加动作的项目');
            $data['actTitle'] || showError('请输入动作主标题');
            if ($actId){//修改
                if ($this->proActModel->edit(array('actId'=>$actId), $data)){
                    showSuccess('编辑动作成功');
                }else{
                    showError('编辑动作失败');
                }
            }else{
                $data['createUserId'] = $this->user['userId'];
                $data['createTime'] = time();
                if ($this->proActModel->add($data)){
                    showSuccess('新增动作成功');
                }else{
                    showError('新增动作失败');
                }
            }
        }else{
            $actData = array();
            $proId = (int)$this->input->get('proId');
            $actId = (int)$this->input->get('actId');
            if (!$proId && !$actId){
                showError('非法参数');
            }
            $maxOrderNum = $this->proActModel->getMax(array('proId'=>$proId),'orderNum');
            $actData['orderNum'] = $maxOrderNum +1;
            if ($actId){
                $actData = $this->proActModel->getData(array('actId'=>$actId));
                $actData || showError('动作数据不存在');
                $proId = $actData['proId'];
            }
            $this->viewData['proId'] = $proId;
            $this->viewData['actData'] = $actData;
            $this->load->view('AdminProAct/add', $this->viewData);
        }
    }
    /**
     * 删除动作
     */
    function del() {
        $actId = (int)$this->input->post('actId');
        $actId || showError('请选择要删除的动作');
        $this->load->model('proActModel');
        if ($this->proActModel->edit(array('actId'=>$actId), array('isDel'=>1))){
            showSuccess('删除动作成功！');
        }else{
            showError('删除动作失败！');
        }
    }
    /**
     * 新增题目
     */
    function itemAdd(){
        $actItemType = $this->config->item('actItemType');
        if ($_POST){
            $itemId = (int)$this->input->post('itemId');
            $data = array(
                'proId' => (int)$this->input->post('proId'),
                'actId' => (int)$this->input->post('actId'),
                'type' => (int)$this->input->post('type'),
                'itemTitle' => $this->input->post('itemTitle'),
                'itemDesc' => $this->input->post('itemDesc'),
                'isRequired' => (int)$this->input->post('isRequired'),
                'orderNum' => (int)$this->input->post('orderNum'),
                'isDel' => 0,
            );
            $data['proId'] || showError('请输入题目所属项目');
            $data['actId'] || showError('请输入题目所属动作');
            isset($actItemType[$data['type']]) || showError('您选择的题目类型不存在');
            isset($_POST['isRequired']) || showError('请选择是否必填项');
            $otherData = array();
            //单选和多选题
            if (in_array($data['type'], array(3,4))){
                $options = array();
                $postOptions = $this->input->post('options');
                if ($postOptions && is_array($postOptions)){
                    foreach ($postOptions as $v){
                        $v && $options[] = trim($v);
                    }
                }
                ($options && count($options) >= 2) || showError('题目选项应大于等于2个');
                $otherData['options'] = $options;
            }
            //时间题
            if (in_array($data['type'], array(5))){
                $postLimit = $this->input->post('limit');
                $otherData['type'] = (int)$postLimit['type'];
                $otherData['startTime'] = $postLimit['startTime'];
                $otherData['endTime'] = $postLimit['endTime'];
                if ($otherData['type'] == 1 && (!$otherData['startTime'] || !$otherData['endTime'])){
                    showError('请选择开始和结束时间');
                }
            }
            //其他数据以json格式存储
            $data['otherData'] = serialize($otherData);
            $this->load->model('proActItemModel');
            if ($itemId){
                if ($this->proActItemModel->edit(array('itemId'=>$itemId), $data)){
                    showSuccess('添加题目成功');
                }else{
                    showError('添加题目失败');
                }
            }else{
                $data['createUserId'] = $this->user['userId'];
                $data['createTime'] = time();
                if ($this->proActItemModel->add($data)){
                    showSuccess('编辑题目成功');
                }else{
                    showError('编辑题目失败');
                }
            }
        }else{
            $actId = (int)$this->input->get('actId');
            $itemId = (int)$this->input->get('itemId');
            if (!$actId && !$itemId){
                $actId || showError('请选择要添加题目的动作');
                $itemId || showError('请选择要编辑的题目');
            }
            if ($actId){
                $this->load->model('proActModel');
                $actData = $this->proActModel->getData(array('actId'=>$actId));
                $actData || showError('动作数据不存在');
                $this->viewData['actId'] = $actData['actId'];
                $this->viewData['proId'] = $actData['proId'];
            }
            if ($itemId){
                $this->load->model('proActItemModel');
                $itemData = $this->proActItemModel->getNewData(array('itemId'=>$itemId));
                $itemData || showError('题目数据不存在');
                $this->viewData['itemId'] = $itemData['itemId'];
                $this->viewData['actId'] = $itemData['actId'];
                $this->viewData['proId'] = $itemData['proId'];
                $this->viewData['itemData'] = $itemData;
            }
            $this->viewData['actItemType'] = $actItemType;
            $this->load->view('AdminProAct/itemAdd', $this->viewData);
        }
    }
    /**
     * 删除题目
     */
    function itemDel() {
        $itemId = (int)$this->input->post('itemId');
        $itemId || showError('请选择要删除的题目');
        $this->load->model('proActItemModel');
        if ($this->proActItemModel->edit(array('itemId'=>$itemId), array('isDel'=>1))){
            showSuccess('删除题目成功！');
        }else{
            showError('删除题目失败！');
        }
    }
}